package org.zhigong.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.zhigong.dto.SysResult;
import org.zhigong.dto.SysSearch;
import org.zhigong.entity.Yonghu;
import org.zhigong.service.YonghuService;

import java.util.List;

@Controller
public class YonghuController {
    
    @Autowired
    private YonghuService yonghuService;

    // 只做跳转用,初始化使用json获取数据处理.
    @RequestMapping(value = "/yonghus", method = RequestMethod.GET)
    public String init(){
        return "yonghu";
    }

    // 分页查询获取列表,返回json
    @RequestMapping(
            value = "/yonghus",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<List<Yonghu>> search(SysSearch sysSearch){
        SysResult<List<Yonghu>> sysResult;

        Yonghu yonghu = new Yonghu();
        String condS = sysSearch.getCondition();
        yonghu.setYhm(condS);
        yonghu.getPage().setCurrentPage(sysSearch.getCurrentPage());
        yonghu.getPage().setPageNumber(sysSearch.getPageNumber());

        try{
            List<Yonghu> yonghuList = yonghuService.getYonghuList(yonghu);
            if(yonghuList.size() > 0){
                yonghuList.get(0).setPage(yonghu.getPage());
            }
            sysResult = new SysResult<List<Yonghu>>(true, yonghuList);
        }catch (Exception e){
            sysResult = new SysResult<List<Yonghu>>(false, e.getMessage());
        }

        return sysResult;
    }

    // 验证用户是否存在
    @RequestMapping(
            value = "/yonghus/{yhm}/varify",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> varify(@PathVariable("yhm") String yhm){
        SysResult<Integer> sysResult;
        try{
            Boolean result = yonghuService.varify(yhm);
            sysResult = new SysResult<Integer>(true, result ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 新增
    @RequestMapping(
            value = "/yonghus",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> create(@RequestBody Yonghu yonghu){
        SysResult<Integer> sysResult;
        try{
            Boolean add  = yonghuService.addYonghu(yonghu);
            sysResult = new SysResult<Integer>(true, add ? 1 : 0);
        }catch (Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }


    // 删除
    @RequestMapping(
            value = "/yonghus/{yhm}",
            method = RequestMethod.DELETE,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> delete(@PathVariable("yhm") String yhm){
        SysResult<Integer> sysResult;
        try{
            Boolean del = yonghuService.deleteYonghu(yhm);
            sysResult = new SysResult<Integer>(true, del ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 更新
    @RequestMapping(
            value = "/yonghus/{yhm}",
            method = RequestMethod.PUT,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> update(@RequestBody Yonghu yonghu){
        SysResult<Integer> sysResult;
        try{
            Boolean upd = yonghuService.updateYonghu(yonghu);
            sysResult = new SysResult<Integer>(true, upd ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 查看
    @RequestMapping(
            value = "/yonghus/{yhm}",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Yonghu> view(@PathVariable("yhm") String yhm){
        SysResult<Yonghu> sysResult;
        try{
            Yonghu yonghu = yonghuService.getYonghuByYhm(yhm);
            sysResult = new SysResult<Yonghu>(true, yonghu);
        }catch(Exception e){
            sysResult = new SysResult<Yonghu>(false, e.getMessage());
        }
        return sysResult;
    }
    
}
